//
//  EFSearchResultCell.m
//  EnjogFood
//
//  Created by 王芳 on 17/5/30.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFSearchResultCell.h"
#import "EFSepecialItem.h"
#import "iToast.h"

@interface EFSearchResultCell ()
@property (strong, nonatomic) UIImageView *iconImgView;
@property(nonatomic, strong)UILabel *productNameLabel;
@property(nonatomic, strong)UILabel *priceLabel;
@property(nonatomic, strong)UILabel *discountPriceLabel;
@property(nonatomic, strong)UIButton *addButton;
@property(nonatomic, strong)UIButton *reduceButton;
@property(nonatomic, strong)UILabel *countLabel;
@property(nonatomic, strong)UIView *bottomLine;

@property (strong, nonatomic) EFSepecialItem *specialItem;
@end

@implementation EFSearchResultCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.backgroundColor =  [UIColor whiteColor];
        [self initView];

    }
    return self;
}

- (void)initView{
    [self.contentView addSubview:self.iconImgView];
    [self.contentView addSubview:self.productNameLabel];
    [self.contentView addSubview:self.priceLabel];
    [self.contentView addSubview:self.discountPriceLabel];
    [self.contentView addSubview:self.addButton];
    [self.contentView addSubview:self.countLabel];
    [self.contentView addSubview:self.reduceButton];
    [self.contentView addSubview:self.bottomLine];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self refreshCountUI];
    self.productNameLabel.top = self.iconImgView.top + 6;
    self.productNameLabel.left = self.iconImgView.right + scale(8.f);
    
    self.priceLabel.left = self.productNameLabel.left;
    self.priceLabel.bottom = self.iconImgView.bottom - 13;
    
    self.discountPriceLabel.left = self.priceLabel.right + scale(8.f);
    self.discountPriceLabel.bottom = self.iconImgView.bottom - 13;
    
    self.bottomLine.bottom = self.height;
}

- (void)setObject:(EFSepecialItem *)item{
    self.specialItem = item;
    [self.iconImgView setImageWithURL:[NSURL URLWithString:item.img]];
    self.productNameLabel.text = item.productName;
    self.productNameLabel.width = kScreenWidth - self.iconImgView.width - scale(58.f);
    [self.productNameLabel sizeToFit];
    if (self.productNameLabel.height > self.iconImgView.height - self.addButton.height - 5.f) {
        self.productNameLabel.height = self.iconImgView.height - self.addButton.height - 5.f;
    }
    
    self.priceLabel.text = item.salePrice;
    [self.priceLabel sizeToFit];
    
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};;
    NSString* strSalePrice = [NSString stringWithFormat:@"¥%@",item.price];
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:strSalePrice attributes:attribtDic];
    self.discountPriceLabel.attributedText = attribtStr;
    [self.discountPriceLabel sizeToFit];
  //  self.discountPriceLabel.text = item.price;
    [self.discountPriceLabel sizeToFit];

}

- (UILabel *)productNameLabel{
    if (!_productNameLabel) {
        _productNameLabel = [[UILabel alloc] init];
        _productNameLabel.backgroundColor = [UIColor clearColor];
        _productNameLabel.font = [UIFont systemFontOfSize:15.f];
        _productNameLabel.textColor = kBlack333;
        _productNameLabel.numberOfLines = 0;
    }
    return _productNameLabel;
}

- (UILabel *)priceLabel{
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.backgroundColor = [UIColor clearColor];
        _priceLabel.font = [UIFont systemFontOfSize:13.f];
        _priceLabel.textColor = RGBCOLOR(0x69BA45);
    }
    return _priceLabel;
}


- (UILabel *)discountPriceLabel{
    if (!_discountPriceLabel) {
        _discountPriceLabel = [[UILabel alloc] init];
        _discountPriceLabel.backgroundColor = [UIColor clearColor];
        _discountPriceLabel.font = [UIFont systemFontOfSize:10.f];
        _discountPriceLabel.textColor = RGBCOLOR(0xCCCCCC);
    }
    return _discountPriceLabel;
}

- (UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.backgroundColor = [UIColor clearColor];
        _countLabel.font = [UIFont systemFontOfSize:13.f];
        _countLabel.textColor = kBlack333;
    }
    return _countLabel;
}

- (UIButton *)reduceButton{
    if (!_reduceButton) {
        _reduceButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20.f, 19.f)];
        [_reduceButton setImage:IMG(@"cutback_icon_default") forState:UIControlStateNormal];
        [_reduceButton addTarget:self action:@selector(onClickReduceBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reduceButton;
}

- (UIButton *)addButton{
    if (!_addButton) {
        _addButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20.f, 19.f)];
        [_addButton setImage:IMG(@"buy_icon_home_default") forState:UIControlStateNormal];
        [_addButton addTarget:self action:@selector(onClickAddBtn) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _addButton;
}

- (UIView *)bottomLine{
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(102, 0, kScreenWidth - 102, 0.5f)];
        _bottomLine.backgroundColor = RGBCOLOR(0xECECEC);
    }
    return _bottomLine;
}

- (UIImageView *)iconImgView{
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 102.f, 102.f)];
        _iconImgView.backgroundColor = RGBCOLOR(0xf3f3f3);
    }
    return _iconImgView;
}

- (void)onClickReduceBtn{
    NSInteger count = self.countLabel.text.integerValue;
    count --;
    if (count <= 0) {
        count = 0;
    }
    
    self.countLabel.text = @(count).stringValue;
    [self.countLabel sizeToFit];
    if (self.countLabel.width < 10) {
        self.countLabel.width = 10.f;
    }
    [self refreshCountUI];
    //进行服务器请求
    if (APP_CONTEXT.accountManager.accountInfo.token.length > 0) {
        [APP_CONTEXT.net subCart:self.specialItem.id qty:[NSNumber numberWithInt:1] type:[NSNumber numberWithInt:0] barcode:self.specialItem.barcode OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
            if (code != 0) {
                [[iToast makeText:message] show];
            }
        }];
    }else{
        //本地减少
        [APP_CONTEXT.shoppingCartManager subStorProduct:self.specialItem];
    }
}

- (void)onClickAddBtn{
    NSInteger count = self.countLabel.text.integerValue;
    count ++;
    self.countLabel.text = @(count).stringValue;
    [self.countLabel sizeToFit];
    if (self.countLabel.width < 10) {
        self.countLabel.width = 10.f;
    }
    [self refreshCountUI];
    //进行服务器请求
    if (APP_CONTEXT.accountManager.accountInfo.token.length > 0) {
        [APP_CONTEXT.net AddCartId:self.specialItem.id qty:[NSNumber numberWithInt:1] type:[NSNumber numberWithInt:0] OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
            if (code != 0) {
                [[iToast makeText:message] show];
            }else{
                 [[iToast makeText:@"成功加入购物车"] show];
            }
        }];
    }else{
        //存在本地 当用户点击购物车时 sync一下
        [APP_CONTEXT.shoppingCartManager addToStoreProduct:self.specialItem];
         [[iToast makeText:@"成功加入购物车"] show];
    }
}

- (void)refreshCountUI{
    self.addButton.right = kScreenWidth - scale(20.f);
    self.addButton.bottom = self.iconImgView.bottom - 13;
    self.countLabel.right = self.addButton.left - scale(10.f);
    self.countLabel.centerY = self.addButton.centerY;
    self.reduceButton.right = self.countLabel.left - scale(10.f);
    self.reduceButton.bottom = self.addButton.bottom;
}

@end
